let lever = [5, 7, 9, 11, 13, 15];

cc.Class({
    extends: cc.Component,

    properties: {
        gamepanel: cc.Node,
        rockerbg: cc.Node,
        role: cc.Node,
        monster: cc.Node,
        monsterPosNode: cc.Node,
        clickLayer: cc.Node,
        bulletLayer: cc.Node,
        rewardLayer: cc.Node,
        dieLayer: cc.Node,
    },
    onLoad() {
        //1开启碰撞管理器
        //给参与碰撞的一方添加脚本并添加oncolisionManager
        let cm = cc.director.getCollisionManager();//开启碰撞管理器
        cm.enabled = true;//开启，只有在onload中有效
        cm.enabledDebugDraw = true;//渲染碰撞组件
    },
    onTouchStart(event) {
        //该组件坐标是居中的
        let pos = event.getLocation();
        let x = pos.x - cc.winSize.width / 2;
        let y = pos.y - cc.winSize.height / 2;
        this.rockerbg.x = x;
        this.rockerbg.y = y;
        this.rockerbg.opacity = 255;
    },
    onTouchMove(event) {
        let pos = event.getLocation();
        pos = this.rockerbg.convertToNodeSpaceAR(pos);
        let Z = Math.sqrt(pos.x * pos.x + pos.y * pos.y);
        let z = this.rockerbg.width / 2;
        let x = pos.x;
        let y = pos.y;
        if (Z > z) {
            x = pos.x * z / Z;
            y = pos.y * z / Z;
        }
        //设置的是父节点的坐标
        this.rockeritem.setPosition(x, y);
        //获取遥感移动方向
        if (y == 0) {
            y = 1;
        }
        let angel = Math.atan(pos.x / pos.y) * 180 / Math.PI;
        if (y < 0)
            angel += 180;
        this.role.script.setMoveDirection(angel);
    },
    onTouchEnd(event) {
        this.rockeritem.setPosition(0, 0);
        this.rockerbg.opacity = 100;
        this.rockerbg.x = 0;
        this.rockerbg.y = -400;
        this.role.script.synStop();
    },
    start() {
        this.rockeritem = this.rockerbg.getChildByName("rockeritem");
        this.clickLayer.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.clickLayer.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        this.clickLayer.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
        this.mstBlLayer = cc.find("Canvas/gamepanel/mstBullet");
        g_Hurt.setLayer(this.monster);
        g_Monster.setLayer(this.monster);
        g_Monster.setMonsterPosNode(this.monsterPosNode);
        g_Monster.setDieLayer(this.dieLayer);
        g_Bullet.setLayer(this.bulletLayer);
        g_MstBullet.setLayer(this.mstBlLayer);
        g_Music.playBGM("bgm");
        window.game = this;

        this.roundLb = cc.find("Canvas/UILayer/top/roundnum").getComponent(cc.Label);
        this.mstNumLb = cc.find("Canvas/UILayer/top/monsternum").getComponent(cc.Label);
        this.pause = false;
        this.UILayer = cc.find("Canvas/UILayer");
        this.round = 1;
        g_Monster.launchMonster();//该函数会调用round，不能把round放在下面

        this.kill = 0;
        this.live = 0;
        this.skillInfo = null;//用于保存技能等级信息，也许放在role里面更好
        this.exp = 0;//经验
        this.lv = 1;
        this.expBar = cc.find("Canvas/UILayer/top/expBar").getComponent(cc.ProgressBar);//获取进度条组件
        this.expLb = cc.find("Canvas/UILayer/top/expBar/exp").getComponent(cc.Label);//获取经验文字组件
        this.lvLb = cc.find("Canvas/UILayer/top/expBar/lv").getComponent(cc.Label);//获取等级组件
        this.expLb.string = 0 + "/" + lever[0];//进度条文字初始化
        this.expBar.progress = 0;
    },
    addKill() {
        this.kill++;
    },
    getRound() {
        return this.round;
    },
    getRole() {
        return this.role;
    },
    getBulletType() {
        return "1";
    },
    getBulletSpeed() {
        return 5;
    },

    flashUI() {
        let info = g_Monster.getMonsterNum();
        this.roundLb.string = "rount" + this.round;
        this.mstNumLb.string = "怪物" + info.monsternum + "/" + info.monsternummax;
    },
    chickRound() {
        //首先获取生成以及怪物的数量
        let { monsternum, monsternummax } = g_Monster.getMonsterNum();
        let monsterListnum = Object.keys(g_Monster.getAllMonster());
        //如果怪物全部生成以及场上怪物为0时进入下一个回合
        if (monsternum == monsternummax && monsterListnum == 0) {
            this.pause = true;
            let pre = g_Res.getRes("ui", "panelSkillSelect");
            let panel = cc.instantiate(pre);
            this.UILayer.addChild(panel);
        }
    },
    //拾取道具
    pickItem() {
        let itemLayer = this.rewardLayer;
        for (let child of itemLayer.children) {
            let tPos = child.getPosition();
            let rPos = this.role.getPosition();
            if (child.picked == true) {
                let v = rPos.sub(tPos);
                let nv = v.normalize();
                let dv = nv.mul(20);
                if (v.mag() > dv.mag()) {
                    v = dv;
                    child.x += v.x;
                    child.y += v.y;
                } else {
                    this.exp++;
                    this.refreshEx();
                    child.destroy();
                }
            } else {
                if (rPos.sub(tPos).mag() <= 300) {
                    child.picked = true;
                }
            }
        }
    },
    refreshEx() {
        if (this.lv == 6 && this.exp >= 15) {
            this.expLb.string = "max";
            return;
        }
        let texp = lever[this.lv - 1];
        this.expBar.progress = this.exp / texp;
        this.expLb.string = this.exp + "/" + texp;
        this.lvLb.string = this.lv;
        if (this.exp >= texp) {
            this.exp = 0;
            this.expBar.progress = 0;
            this.lv++;
            texp = lever[this.lv - 1];
            this.expLb.string = this.exp + "/" + texp;
            this.lvLb.string = this.lv;
        }
    },
    nextRound() {
        this.round++;
        this.pause = false;
        g_Monster.launchMonster();
    },
    setSkillInfo(skillInfo) {
        cc.log(1111111111, skillInfo);
        this.skillInfo = skillInfo;
    },
    getSkillInfo() {
        cc.log(222222222, this.skillInfo);
        return this.skillInfo;
    },
    gameOver() {
        this.pause = true;
        let rsView = g_Res.getRes("prefab", "ResultView");
        let rs = cc.instantiate(rsView);
        this.UILayer.addChild(rs);
    },
    getResultInfo() {
        return {
            round: this.round,
            kill: this.kill,
            live: this.live
        };
    },
    onClear() {
        g_Monster.clear();
        g_Bullet.clear();
        g_CAIManager.clear();
        g_Hurt.clear();
    },
    update(dt) {
        if (this.pause) {
            return;
        }
        this.live += dt;//增加生存时间
        g_Hurt.update(dt);
        g_Monster.update(dt);
        g_CAIManager.update(dt);
        g_Bullet.update(dt);
        g_MstBullet.update(dt);
        //g_MSTDebuff.update(dt);
        g_Buff.update(dt);
        this.flashUI();
        this.chickRound();
        this.pickItem();
        //this.checkRoleHP();
    },
});

